/**
 * User: alesj
 * Date: 2004.9.6
 * Time: 12:49:38
 */

package com.generalynx.ecos.data.dao;

import com.generalynx.common.data.SpecialDate;
import com.generalynx.common.data.dao.IJdbcDAO;
import com.generalynx.common.utils.DateData;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class JdbcDAOImpl extends JdbcDaoSupport implements IJdbcDAO {

    private String m_lockHint;

    public String getLockHint() {
        return m_lockHint;
    }

    public void setLockHint(String lockHint) {
        m_lockHint = lockHint;
    }

    private String createSaveQuery(boolean newEntity, String table, String[] fields, Object[] values) {
        StringBuffer buffer = new StringBuffer();
        buffer.append(newEntity ? "INSERT INTO " : "UPDATE ").append(table);
        if (getLockHint() != null) {
            buffer.append(" WITH(").append(getLockHint()).append(")");
        }
        buffer.append(newEntity ? " (" : " SET ");
        for (int i = 0; i < fields.length; i++) {
            if (i > 0) {
                buffer.append(",");
            }
            buffer.append(fields[i]);
            if (!newEntity){
                buffer.append(" = ?");
            }
        }
        if (newEntity) {
            buffer.append(") VALUES(");
            for (int i = 0; i < values.length; i++) {
                if (i > 0) {
                    buffer.append(",");
                }
                buffer.append("?");
            }
            buffer.append(")");
        }
        String query = buffer.toString();
        logger.debug(query);
        return query;
    }

    private class IntArrayHandler implements RowCallbackHandler {
        private List list = new ArrayList();

        public void processRow(ResultSet rs) throws SQLException {
            list.add(new Integer(rs.getInt(1)));
        }

        public int[] getArray() {
            int[] array = new int[list.size()];
            for (int i = 0; i < array.length; i++) {
                array[i] = ((Integer)list.get(i)).intValue();
            }
            return array;
        }

    }

    private int[] getIntArray(String query, int intArg) throws DataAccessException {
        IntArrayHandler intArrayHandler = new IntArrayHandler();
        getJdbcTemplate().query(query, new Object[]{new Integer(intArg)}, intArrayHandler);
        return intArrayHandler.getArray();
    }

    /* public jdbc DAO methods */

    public SpecialDate findHourShifting(final DateData dateData)
            throws DataAccessException {
        String dateString = "'" + dateData.format(new SimpleDateFormat("yyyy-MM-dd")) + "'";
        return (SpecialDate)getJdbcTemplate().query("SELECT kpz_vrsta, kpz_od" +
                                                    " FROM koledarpz" +
                                                    " WHERE " + dateString + " >= kpz_od " +
                                                    " AND " + dateString + " <= kpz_do",
                                                    new ResultSetExtractor() {
            public Object extractData(ResultSet rs) throws SQLException, DataAccessException {
                if (!rs.next()) {
                    throw new IllegalArgumentException("No such data in koledarpz: " + dateData);
                }
                DateData result = new DateData(rs.getTimestamp(2));
                //result
                return new SpecialDate("p".equalsIgnoreCase(rs.getString(1)), dateData.isEqual(result));
            }
        });
    }

}
